﻿using System;
using System.Collections.Generic;
using System.Text;
using Stauffware.Common.Core;
using Stauffware.Common.Time;
using System.Globalization;
using NUnit.Framework;

namespace UnitTests.Time
{
	public class TimeParseTests
	{
		//=============================================================================
		// Tests that yield a value
		//
		//          input          parsed text     value H,M,S,F     format

		[TestCase( "12:34:56pm",   "12:34:56pm",   12, 34, 56, 0,   "hh:mm:sstt",
						TimeParseOptions.AllowSeconds)]
		[TestCase( "12:34am",      "12:34am",       0, 34, 0, 0,    "hh:mmtt",
						TimeParseOptions.None)]
		[TestCase( "23:45:01",     "23:45:01",      23, 45, 1, 0,   "HH:mm:ss",
						TimeParseOptions.AllowSeconds)]
		[TestCase( "0:0",          "0:0",           0, 0, 0, 0,     "H:m",
						TimeParseOptions.None)]

		public static void ValueTest (string input,
									string expectedParsedText,
									int expectedHour,
									int expectedMinute,
									int expectedSecond,
									int expectedMillisecond,
									string expectedFormat,
									TimeParseOptions options)
		{
			DateTime expectedValue = new DateTime(1, 1, 1, 
					expectedHour, expectedMinute, expectedSecond, expectedMillisecond);

			TimeParse parse = new TimeParser(options).Parse(input);

			if (parse.GotValue)
				NUnit.Framework.Assert.AreEqual(expectedValue, parse.Value, "Value");

			NUnit.Framework.Assert.AreEqual(expectedParsedText, parse.ParsedText, "ParsedText");
			NUnit.Framework.Assert.AreEqual(expectedFormat, parse.Format,"Format");
			NUnit.Framework.Assert.AreEqual(null, parse.ErrorMessage,"ErrorMessage");
			NUnit.Framework.Assert.AreEqual(true, parse.GotValue, "GotValue");
			NUnit.Framework.Assert.AreEqual(false, parse.IsError, "IsError");
			NUnit.Framework.Assert.AreEqual(false, parse.IsEmpty, "IsEmpty");
		}

		//=============================================================================
		// Tests that yield an error.
		//
		//         input     parsed text    error 

		[TestCase( " ",      "",            "invalid time; expecting digits",
						TimeParseOptions.ParseEntireInput)]

		public static void ErrorTest (string input,
									string expectedParsedText,
									string expectedError,
									TimeParseOptions options)
		{
			TimeParse parse = new TimeParser(options).Parse(input);

			NUnit.Framework.Assert.AreEqual(expectedParsedText, parse.ParsedText, "ParsedText");
			NUnit.Framework.Assert.AreEqual(expectedError, parse.ErrorMessage, "ErrorMessage");
			NUnit.Framework.Assert.AreEqual(false, parse.GotValue, "GotValue");
			NUnit.Framework.Assert.AreEqual(true, parse.IsError, "IsError");
			NUnit.Framework.Assert.AreEqual(false, parse.IsEmpty, "IsEmpty");
		}

		//=============================================================================
		// Tests that yield "empty".
		//
		//         input     parsed text    options 

		[TestCase( "",       "",            TimeParseOptions.None)]
		[TestCase( "  ",     "",            TimeParseOptions.None)]
		[TestCase( "",       "",            TimeParseOptions.ParseEntireInput)]

		public static void EmptyTest (string input,
									string expectedParsedText,
									TimeParseOptions options)
		{
			TimeParse parse = new TimeParser(options).Parse(input);

			NUnit.Framework.Assert.AreEqual(expectedParsedText, parse.ParsedText, "ParsedText");
			NUnit.Framework.Assert.AreEqual(null, parse.ErrorMessage, "ErrorMessage");
			NUnit.Framework.Assert.AreEqual(false, parse.GotValue, "GotValue");
			NUnit.Framework.Assert.AreEqual(false, parse.IsError, "IsError");
			NUnit.Framework.Assert.AreEqual(true, parse.IsEmpty, "IsEmpty");
		}

		//=============================================================================
	}
}
